#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"
#include "Game/GameState.h"
#include "Game/Component/MultiModel.h"
#include "PreRenderProcess.h"
#include "SsrProcess.h"


void LocalScene::OnInit()
{
	mCamera.SetSpeed(4.f);

	mPreRenderProcess = GetProcess<PreRenderProcess>("PreRenderProcess");

	mSsrProcess = GetProcess<SsrProcess>("SsrProcess");

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");



	GeometryGenerator geometryGenerator;
	GeometryGenerator::MeshData cubeData = geometryGenerator.CreateCube(1.2f, 1.2f, 1.2f, 1);
	GeometryGenerator::MeshData panelData = geometryGenerator.CreatePanel(6.f, 6.f, glm::vec3(0.f, 1.f, 0.f));


	LocalEntity* woodEntity = CreateEntity<LocalEntity>("wood");
	woodEntity->AddComponent<Model>(cubeData, true);
	woodEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.7f, 0.f));
	woodEntity->GetTransform().UpdateMatrices();

	woodEntity->mAlbedoPath = "Pbr\\Wood_Albedo.png";
	woodEntity->mNormalPath = "Pbr\\Wood_Normal.png";
	woodEntity->mMetallicPath = "Pbr\\Wood_Metallic.png";
	woodEntity->mRoughnessPath = "Pbr\\Wood_Roughness.png";

	woodEntity->mHasNormal = true;



	LocalEntity* metalEntity = CreateEntity<LocalEntity>("metal");
	metalEntity->AddComponent<Model>(panelData, true);

	metalEntity->mAlbedoPath = "Pbr\\Metal_Albedo.png";
	metalEntity->mNormalPath = "Pbr\\Metal_Normal.png";
	metalEntity->mMetallicPath = "Pbr\\Metal_Metallic.png";
	metalEntity->mRoughnessPath = "Pbr\\Metal_Roughness.png";

	metalEntity->mHasNormal = false;



	mLightRadius = 2.5f;
	mLightIntensity = 10.f;
	mLightColor = glm::vec3(1.f);
	mLightTransform = glm::translate(glm::mat4(1.0f), glm::vec3(2.5f, 2.f, -6.f));
}

void LocalScene::OnTick(float deltaTime)
{
	mLightPosition = glm::vec3(mLightTransform[3][0], mLightTransform[3][1], mLightTransform[3][2]);

	/*static float countTime = 0;
	countTime += deltaTime;
	if (countTime > 2.f)
	{
		glm::vec3 cameraPos = mCamera.GetPosition();
		LOGI(L"CameraPosition %f, %f, %f", mLightPosition.x, mLightPosition.y, mLightPosition.z);
		countTime = 0.f;
	}*/
}

void LocalScene::OnImgui()
{
	if (ImGui::BeginMainMenuBar())
	{
		ImGui::Text("Fps : %d ", GRenderState->GetFps());

		/*ImGui::RadioButton("Default", &mForwardPlusProcess->mPushConstants.debugIndex, 0);
		ImGui::RadioButton("HeatMap", &mForwardPlusProcess->mPushConstants.debugIndex, 1);
		ImGui::RadioButton("HeatMap Only", &mForwardPlusProcess->mPushConstants.debugIndex, 2);
		ImGui::RadioButton("PreDepth", &mForwardPlusProcess->mPushConstants.debugIndex, 3);
		ImGui::RadioButton("Normal", &mForwardPlusProcess->mPushConstants.debugIndex, 4);*/

		ImGui::EndMainMenuBar();
	}



	ImGui::Checkbox("Enable SSR", &mSsrProcess->mEnableSSR);
	ImGui::SliderFloat("distance bias", &mSsrProcess->mPushConstants.distanceBias, 0.0001, 0.5f);//temp sol
	ImGui::SliderFloat("rayStep", &mSsrProcess->mPushConstants.rayStep, 0.01, 1.0f);
	ImGui::SliderInt("iteration count", &mSsrProcess->mPushConstants.iterationCount, 1, 500);
	ImGui::Checkbox("Enable Exponential step", &mSsrProcess->mIsExponentialStepEnabled);

	bool adaptiveStepEnabled = mSsrProcess->mIsAdaptiveStepEnabled;
	ImGui::Checkbox("Enable adaptive step", &adaptiveStepEnabled);
	ImGui::Checkbox("Enable binary step", &mSsrProcess->mIsBinarySearchEnabled);
	if (adaptiveStepEnabled && !mSsrProcess->mIsAdaptiveStepEnabled) 
		mSsrProcess->mIsBinarySearchEnabled = false;
	mSsrProcess->mIsAdaptiveStepEnabled = adaptiveStepEnabled;
	if (mSsrProcess->mIsBinarySearchEnabled) 
		mSsrProcess->mIsAdaptiveStepEnabled = false;

	ImGui::Checkbox("Enable sampling", &mSsrProcess->mIsSamplingEnabled);
	ImGui::Checkbox("debugDraw", &mSsrProcess->mDebugDraw);
	if (mSsrProcess->mIsSamplingEnabled) {
		ImGui::SliderInt("sample count", &mSsrProcess->mPushConstants.sampleCount, 1, 16);
		ImGui::SliderFloat("sampling coefficient", &mSsrProcess->mPushConstants.samplingCoefficient, 0.f, 0.5f);
	}


	ImGui::ColorEdit3("Color", &mLightColor.x);
	ImGui::InputFloat("Intensity", &mLightIntensity);
	ImGui::SliderFloat("Radius", &mLightRadius, 0.0f, 10.0f);

	glm::vec3 position;
	glm::vec3 rotation;
	glm::vec3 scale;

	ImGuizmo::BeginFrame();
	ImGuizmo::SetOrthographic(false);

	ImGuizmo::DecomposeMatrixToComponents(&mLightTransform[0][0], &position.x, &rotation.x, &scale.x);

	ImGui::InputFloat3("Position", &position.x);

	ImGuizmo::RecomposeMatrixFromComponents(&position.x, &rotation.x, &scale.x, &mLightTransform[0][0]);

	ImGuizmo::SetRect(0, 0, GRenderState->GetWidth(), GRenderState->GetHeight());
	ImGuizmo::Manipulate(&mCamera.mZmoView[0][0], &mCamera.mProj[0][0], ImGuizmo::TRANSLATE, ImGuizmo::WORLD, &mLightTransform[0][0], NULL, NULL);
}

void LocalScene::OnExit()
{

}